<?php

/**
 * Class to define tags and how they behave
 *
 * @package CryoBB
 * @filename Tag.php
 * @version 1.0.0
 * @author Scott Martin <sjm.dev1[at]gmail[dot]com>
 * @date November 5th, 2010
 * @license GNU GPL V3
 */
class CryoBB_Tag
{
	// Tag name
	protected $tag;
	
	// Tag has parameter?
	protected $parameter = false;
	
	// Use text between tags as parameter?
	protected $useTextAsParameter = false;
	
	// Opening tag
	protected $openTag = '';
	
	// Closing tag
	protected $closeTag = '';
	
	// Parse the contents between bb code tags for additional bbcode tags?
	protected $parseContents = true;
	
	protected $parseParameter = false;
	
	// Processor object (to call functions of)
	protected $processorObject = null;
	
	/**
	 * Sets the tag name
	 *
	 * @access public
	 * @param string $tag - the name of the BB code tag
	 * @return void
	 */
	public function __construct($tag)
	{
		$this->processorObject = new CryoBB_Processor();
		if (!ctype_alnum($tag))
		{
			trigger_error('CryoBB_Tag::__construct() parameter should be alphanumeric', E_USER_WARNING);
			return false;
		}
		
		$this->tag = strtolower($tag);
	}
	
	/**
	 * Sets the text to use as the opening tag
	 * 
	 * @param string $openTag
	 */
	public function setOpenTag($openTag)
	{
		$this->openTag = $openTag;
		return $this;
	}
	
	/**
	 * Sets the text to use as the closing tag
	 * 
	 * @param string $closeTag
	 */
	public function setCloseTag($closeTag)
	{
		$this->closeTag = $closeTag;
		return $this;
	}
	
	/**
	 * Tells this object whether or not to parse the contents between this tags'
	 * open and close for more BB code tags.
	 *
	 * @access public
	 * @param boolean $bool
	 * @return this
	 */
	public function parseContents($bool)
	{
		$this->parseContents = (bool) $bool;
		return $this;
	}
	
	/**
	 * Tells this object whether or not to parse bbcode in the parser
	 * 
	 * @access public
	 * @param boolean $bool
	 * @return this
	 */
	public function parseParameter($bool)
	{
		$this->parseParameter = (bool) $bool;
		return $this;
	}
	
	/**
	 * Gives the object (who's class implements CryoBB_Processor) to the tag
	 * for retrieval by CryoBB_Base::make()
	 * 
	 * @param CryoBB_Processor $processor
	 * @return this
	 */
	public function setProcessor(CryoBB_Processor $processor)
	{
		$this->processorObject = $processor;
		return $this;
	}
	
	/**
	 * Assembles the replacement for CryoBB_Base.  Not meant to be used.
	 * 
	 * @param string $content Text inside of tag
	 * @param string $parameter Parameter on tag (if any)
	 */
	public function build($parameter)
	{
		return array($this->openTag, $this->closeTag);
	}
	
	//accessor for object properties
	public function __get($var)
	{
		return isset($this->$var) ? $this->$var : false;
	}
}